package com.azzcs.juc.demo5;

/**
 * @Author: wzg
 * @Date: 2021/6/15 下午2:39
 */
public class Q {

    private final String[] buffer= new String[10];

    private int productIndex,consumerIndex,size;

    public synchronized void put(String data) throws InterruptedException {
        while (size >= buffer.length){
            wait();
        }
        buffer[productIndex++] = data;
        if(productIndex == buffer.length){
            productIndex = 0;
        }
        size++;
        notifyAll();
    }
    public synchronized String get()throws InterruptedException{
        while (size <= 0){
            wait();
        }
        String result = buffer[consumerIndex++];
        if(consumerIndex == buffer.length){
            consumerIndex = 0;
        }
        size--;
        notifyAll();
        return result;
    }

}
